import { message } from 'antd';
import axios from 'axios'

const baseURL = 'http://localhost:8000'
const request = axios.create({
	baseURL
})

request.interceptors.request.use(
	config => {
		const token = localStorage.getItem("graduation_yzh_token");
		config.headers.authorization = `Bearer ${token}`;
		return config;
	},
	error => {
		// 对请求错误做些什么
		return Promise.reject(error)
	}
)

request.interceptors.response.use(
	(res) => {
		return res.data;
	},
	(err) => {
		if (err && err.response) {
			switch (err.response.status) {
				case 400:
					message.error(err.response.data.message)
					break;
				case 401:
					message.error("请登录");
					window.location.hash = '/'
					break;
				default:
					message.error('其他错误信息')
			}
		}
		return Promise.reject(err);
	}
);

export { request }
